package com.lp.guoguo.controller;

import com.lp.guoguo.common.Result;
import com.lp.guoguo.domain.EventRegistrations;
import com.lp.guoguo.service.EventRegistrationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/api/event-registrations")
@RequiredArgsConstructor
public class EventRegistrationController {

    private final EventRegistrationsService eventRegistrationsService;
    private static final Logger log = LoggerFactory.getLogger(EventRegistrationController.class);

    // 创建新的活动报名
    @PostMapping
    public Result<?> createEventRegistration(@RequestBody EventRegistrations eventRegistration) {
        // 设置报名时间为当前时间
        eventRegistration.setRegistrationDate(new Date());
        boolean success = eventRegistrationsService.save(eventRegistration);
        if (!success) {
            log.error("创建活动报名失败，数据：{}", eventRegistration);
        }
        return success ? Result.success() : Result.failed("创建活动报名失败");
    }

    // 获取所有活动报名
    @GetMapping
    public Result<List<EventRegistrations>> getAllEventRegistrations() {
        List<EventRegistrations> registrations = eventRegistrationsService.list();
        return Result.success(registrations);
    }

    // 根据ID获取单个活动报名
    @GetMapping("/{id}")
    public Result<EventRegistrations> getEventRegistrationById(@PathVariable Integer id) {
        EventRegistrations registration = eventRegistrationsService.getById(id);
        return registration != null ? Result.success(registration) : Result.failed("未找到该活动报名");
    }

    // 更新活动报名信息
    @PutMapping("/{id}")
    public Result<?> updateEventRegistration(@PathVariable Integer id, @RequestBody EventRegistrations eventRegistration) {
        eventRegistration.setId(id);
        boolean success = eventRegistrationsService.updateById(eventRegistration);
        return success ? Result.success() : Result.failed("更新活动报名失败");
    }

    // 删除活动报名
    @DeleteMapping("/{id}")
    public Result<?> deleteEventRegistration(@PathVariable Integer id) {
        boolean success = eventRegistrationsService.removeById(id);
        return success ? Result.success() : Result.failed("删除活动报名失败");
    }
}